/*
 * File Name：CWSystem.java
 * Copyright：Copyright 2008-2015 CiWong.Inc. All Rights Reserved.
 * Description： CWSystem.java
 * Modify By：res-jxliu
 * Modify Date：2015-1-7
 * Modify Type：Add
 */
package com.ciwong.mobilelib.utils;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.ciwong.libs.utils.CWSys;
import com.ciwong.libs.utils.FileUtils;
import com.ciwong.mobilelib.R;
import com.ciwong.mobilelib.widget.CWDialog;

import java.io.File;

/**
 * 习网系统类
 *
 * @author res-jxliu
 * @version ciwong v.1.0 2015-1-7
 * @since ciwong v.1.0
 */
public class BaseSystem extends CWSys {

    /**
     * 项目目录名
     */
    public static String PROJECT_FOLDER = "base";

    /**
     * 不扫描的多媒体文件夹
     */
    protected static final String NO_MEDIA = ".nomedia";

    /**
     * 录音文件存放目录
     */
    public static final String AUDIO_TEMP_DIR = "audiorecord";

    /**
     * 图片文件存放目录   手写存储的的笔迹地址
     */
    public static final String IMAGES_DIR = "images";

    /**
     * 相册
     */
    public static final String PHOTO_ALBUM = "album";

    /**
     * 下载目录
     */
    public static final String DOWNLOAD = "download";

    /**
     * 资源包目录
     */
    public static final String PACKAGES = "packages";


    /**
     * 默认音频文件存放地址
     */
    public static final String AUDIO = "audio";


    /**
     * 防止系统扫描音视频文件
     */
    public static final String FORBID_SYSTEM_SCAN = ".nomedia";

    /**
     * @return 项目根目录
     */
    public static String getProjectFolderPath() {
        final String path = FileUtils.getCiwongRoot() + File.separator
                + PROJECT_FOLDER;
        File f = new File(path);
        if (!f.exists()) {
            new File(f, NO_MEDIA).mkdirs();
        }

        return path;
    }

    /**
     * 获取应用程序包信息
     *
     * @param context 上下文
     * @return {@link PackageInfo}
     */
    public static PackageInfo getSystemPackageInfo(Context context) {
        try {
            return context.getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取版本号
     *
     * @param context 上下文
     * @return 版本号
     */
    public static int getSystemVersionCode(Context context) {
        PackageInfo pi = getSystemPackageInfo(context);
        if (pi != null) {
            return pi.versionCode;
        }
        return -1;
    }

    /**
     * 获取音频默认存放目录,如果创建不成功则返回null
     *
     * @return 默认音频存放目录
     */
    public static final File getAudioFolder() {
        final String path = getSDCardFolder().getAbsolutePath() + File.separator
                + AUDIO;
        File f = new File(path);
        if (!f.exists()) {
            if (!f.mkdirs()) {
                f = null;
            }
        }
        return f;
    }


    /**
     * 获取录音临时文件
     *
     * @return 文件绝对路径
     */
    public static String getAudioRecordPath() {
        final String path = getProjectFolderPath() + File.separator
                + AUDIO_TEMP_DIR;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return f.getAbsolutePath();
    }

    /**
     * 获取临时图片地址
     *
     * @return 临时图片绝对地址
     */
    public static String getImagesPath() {
        String path = getProjectFolderPath() + File.separator + IMAGES_DIR;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return f.getAbsolutePath();
    }

    /**
     * 获取手机拍一拍 涂鸦临时文件
     *
     * @return 图片地址
     */
    public static String getImagePath() {
        String path = getProjectFolderPath() + File.separator + IMAGES_DIR;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }


    /**
     * 获取相册地址
     *
     * @return 相册地址
     */
    public static String getPhotoAlbumPath() {
        String path = getProjectFolderPath() + File.separator + PHOTO_ALBUM;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取资源包下载目录
     *
     * @return 资源包下载目录
     */
    public static String getDownloadPath() {
        String path = getProjectFolderPath() + File.separator + DOWNLOAD;
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }
        return path;
    }

    /**
     * 获取资源包目录
     *
     * @return 资源包目录
     */
    public static String getPackagesPath() {
        String path = getProjectFolderPath() + File.separator + PACKAGES;
        File f = new File(path);
        if (!f.exists()) {
            boolean mkdirs = f.mkdirs();
        }
        return path;
    }

    /**
     * 获取语音识别初始化目录
     *
     * @return 应用要目录
     */
    public static File getSpeekInitDir() {
        File f = new File(getSDCardFolder(),
                "Android" + File.separator + "data" + File.separator
                        + getContext().getPackageName() + File.separator
                        + "files");
        if (!f.exists()) {
            f.mkdirs();
        }
        return f;
    }

    /**
     * 回调接口
     *
     * @author res-jxliu
     */
    public static interface CallBack {
        /**
         * 回调方法
         */
        void callback();
    }

    public static void alertDialog(Activity c, int title, int msg, int confirm,
                                   final CallBack call) {
        CWDialog cwDialog = new CWDialog(c, false, false);
        cwDialog.setTitle(title);
        cwDialog.setTitleTextColor(Color.BLACK);
        cwDialog.setMessage(c.getString(msg),
                (int) c.getResources().getDimension(R.dimen.text_size_default),
                Color.BLACK);
        cwDialog.setPositiveButton(confirm,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        if (call != null) {
                            call.callback();
                        }

                    }
                });
        cwDialog.setNegativeButton(android.R.string.cancel,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
        cwDialog.show();
    }

    public static void makeForbidSystemScanFile() {

        String path = FileUtils.getCiwongRoot() + File.separator + FORBID_SYSTEM_SCAN;
        File file = new File(path);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 通过dp长度获取px长度
     *
     * @param mContext 上下文
     * @param dpLength dp长度getPxByDpi
     * @return px长度
     */
    public static int getPxByDpi(Context mContext, int dpLength) {
        DisplayMetrics dm = getDisPlayMetrics(mContext);
        float scale = dm.density;
        return (int) (dpLength * scale + 0.5f);
    }

    /**
     * 获取displaymetrics
     *
     * @param mContext 上下文
     * @return displaymetrics
     */
    public static DisplayMetrics getDisPlayMetrics(Context mContext) {
        Display display = getDisplay(mContext);
        DisplayMetrics dm = new DisplayMetrics();
        display.getMetrics(dm);
        return dm;
    }

    /**
     * 获取display
     *
     * @param mContext 上下文
     * @return display
     */
    public static Display getDisplay(Context mContext) {
        Display display = ((WindowManager) mContext
                .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        return display;
    }


}
